package cn.edu.njust.alg.leetcode;

import java.util.Stack;

/**
 * 创建时间：2015年6月15日 上午11:47:37 项目名称：AlgorithmExercise
 * 
 * @author Zhou Xiumin
 * @version 1.0
 * @since JDK 1.7.0_21 文件名称：Leetcode_0_t.java 类说明：
 */
public class Leetcode_32_Longest_Valid_Parentheses {

	public static int longestValidParentheses(String s) {
		if (s == null || s.length() == 0) {
			return 0;
		}
		Stack<Character> stack = new Stack<Character>();
		Stack<Integer> sint = new Stack<Integer>();
		int max = 0;
		int pre = 0;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c != '(' && c != ')') {
				continue;
			}
			if (c == '(') {
				stack.push(c);
				if (pre != 0) {
					sint.push(pre);
				}
				pre = 0;

			} else if (c == ')') {
				if (!stack.isEmpty()) {
					stack.pop();
					pre++;
				}

			}
		}
		return max * 2;
	}

	public static void main(String[] args) {
		String[] ps = { ")()())", ")()", "()(()" };
		for (String s : ps) {
			System.out.println(longestValidParentheses(s));
		}
	}

}
